package com.sqllog.admin.sql.domain;

import com.alibaba.fastjson.JSON;
import com.sqllog.admin.sql.mapper.CanalClientConfigInfoMapper;
import com.sqllog.admin.utils.SpringContextUtils;
import com.sqllog.config.CanalClientConfig;
import com.sqllog.entity.ClientRunnableException;
import lombok.extern.slf4j.Slf4j;

/**
 * 客户端 解析消息线程 异常捕捉
 *
 * @author chennengcheng
 * @date 2022-01-21 15:53
 * @remark
 */

@Slf4j
public class ClientRunThreadException implements Thread.UncaughtExceptionHandler {

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        if (e instanceof ClientRunnableException) {
            log.error("错误数据: " + ((ClientRunnableException) e).getData() + "错误消息: " + e.getMessage());
            String data = ((ClientRunnableException) e).getData();
            CanalClientConfig canalClientConfig = JSON.parseObject(data, CanalClientConfig.class);
            CanalClientConfigInfoMapper canalClientConfigInfoMapper = SpringContextUtils.getBean(CanalClientConfigInfoMapper.class);
            canalClientConfigInfoMapper.updateStatusByHostAndName(e.getMessage(), "3", canalClientConfig.getHost(), canalClientConfig.getInstanceName());
        }else {
            throw new RuntimeException(e);
        }
    }
}
